<template>
    <!-- 攻防赛-周期 -->
    <SearchTable
            :columns="col"
            :data="list"
            :total="total"
            :page.sync="query.pageIndex"
            :size.sync="query.pageSize"
            :disabled="(item) => { return item.rest ? true : false}"
            showSelectTag
            :selectName="(item) => {return `周期${item.number}`}"
            :selection.sync="selectList"
            @queryFun="getCycleList">
        <template slot="button-group">
            <!-- <Checkbox v-model="checkAll" :disabled="checkAllDisabled" @on-change="handleCheckAll">全选</Checkbox> -->
            <Poptip title="周期重置，将不可逆，确认继续?" confirm transfer @on-ok="batchReset">
                <Button type="error" ghost :disabled="selectList.length === 0">批量重置</Button>
            </Poptip>
        </template>
    </SearchTable>
</template>

<script>
import SearchTable from '@/components/SearchTable/SearchTable'
import { getCycleList, resetCycle } from '@/api/drill_compete';
export default {
    name: 'CycleStatistics',
    props: ['type'],
    components: {SearchTable},
    inject: ['parent'],
    data() {
        return {
            query: {
                pageIndex: 1,
                pageSize: 10
            },
            list: [],
            total: 0,
            selectList: [],
            sceneId: this.$route.query.id,
            col: [
                {
                    type:'selection',
                    width:60
                },
                {
                    title: '周期序号',
                    key: 'number',
                    minWidth: 60,
                    tooltip: true,
                    placement:"top-left"
                },
                {
                    type: 'Tag',
                    title: '是否重置',
                    key: 'rest',
                    placement:"top-right",
                    filter: (rest) => {
                        return rest ? '是' : '否';
                    },
                    setColor: (rest) => {
                        return rest ? 'var(--status-yes)': 'var(--status-no)';
                    }
                },
                {
                    title: '周期时长',
                    key: 'intervals',
                    minWidth: 60,
                    tooltip: true,
                    filter: (intervals) => {
                        return `${intervals}（分钟）`;
                    }
                },
                {
                    type:"showTime",
                    title: '开始时间',
                    key: 'startTime',
                    minWidth: 60,
                    tooltip: true
                },
                {
                    type:"showTime",
                    title: '结束时间',
                    key: 'nextStartTime',
                    minWidth: 60,
                    tooltip: true
                },
                {
                    title: '攻击成功的团队',
                    key: 'subVOS',
                    minWidth: 60,
                    tooltip: true,
                    filter: (subVOS) => {
                        return subVOS.length > 0
                            ? subVOS
                                  .map((item) => {
                                      return `${item.teamName}-${item.targetName}`;
                                  })
                                  .join(',')
                            : '无';
                    }
                },
                {
                    title: '操作',
                    placement:'top-right',
                    key:'action',
                    width: 80,
                    opList: [
                        {
                            txt: '重置',
                            tooltip: true,
                            title:'周期重置，将不可逆，确认继续？',
                            disabled: (data) => {
                                return data.rest === 1;
                            },
                            img: {
                                disabled:require('@/assets/project/resetPwd-d.png'),
                                default:require('@/assets/project/resetPwd-h.png')
                            },
                            on:{
                                click:(data,e) => {
                                    this.resetCycle([data.id]);
                                }
                            }
                        }
                    ]
                }
            ],
            timeout: {},
            loading:false,
            checkAll:false,
            checkAllDisabled:false
        };
    },
    filters: {},
    beforeDestroy() {
        clearTimeout(this.timeout);
    },
    mounted() {
        this.getCycleList()
    },
    methods: {
        // initData(page, size) {
        //     this.query.pageIndex = page;
        //     this.query.pageSize = size;
        //     this.getCycleList();
        // },
        changePage(page) {
            this.query.pageIndex = page;
            this.getCycleList();
        },
        getCycleList() {
            if (this.$route.query.id) {
                this.loading = true;
                getCycleList(this.sceneId, this.query).then((data) => {
                    if (data.code == 1) {
                        this.list = data.data.items;
                        this.list.forEach((item) => {
                            item.disabled = item.rest ? true : false;
                        });
                        this.total = data.data.total;
                    } else {
                        this.$Message.error(data.message);
                    }
                }).finally(() => {
                    this.loading = false;
                });
            }
        },
        async batchReset() {
            let cycleIds = this.selectList.map((item) => item.id);
            await this.resetCycle(cycleIds);
            this.selectList = [];
        },
        resetCycle(ids) {
            return new Promise((resolve, reject) => {
                resetCycle(this.sceneId, ids).then((data) => {
                    if (data.code == 1) {
                        this.$Message.info('重置成功');
                        this.getCycleList();
                        resolve(1);
                    } else {
                        this.$Message.error(data.message);
                    }
                });
            });
        },
        handleCheckAll(val){
            this.$refs.cardList.selectAll(val)
        }
    }
};
</script>
<style scoped></style>
